<template>
  <transition name="el-zoom-in-center">
    <div class="jsbos-preview-main">
      <div class="jsbos-common-page-header">
        <el-page-header
          @back="goBack"
          :content="!dataForm.id ? '新建' : isDetail ? '详情' : '编辑'"
        />
        <div class="options">
          <el-button type="primary" @click="dataFormSubmit()" v-if="!isDetail">
            保 存
          </el-button>
          <el-button @click="goBack"> 取 消 </el-button>
        </div>
      </div>
      <el-row
        :gutter="15"
        class="main"
        :style="{ margin: '0 auto', width: '1200px' }"
      >
        <el-form
          v-loading="loading"
          ref="elForm"
          :model="dataForm"
          :rules="rules"
          size="small"
          label-width="100px"
          label-position="right"
        >
          <template v-if="!loading">
            <el-col :span="24">
              <el-form-item label="封面" prop="cover">
                <UploadImg
                  v-model="dataForm.cover"
                  :fileSize="5"
                  sizeUnit="MB"
                  :limit="1"
                >
                </UploadImg>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="名称" prop="name">
                <el-input
                  v-model="dataForm.name"
                  placeholder="请输入"
                  clearable
                  :style="{ width: '100%' }"
                >
                </el-input>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="课程目录" prop="category">
                <TreeSelect
                  v-model="dataForm.category"
                  placeholder="请选择"
                  clearable
                  :options="categoryOptions"
                  :props="categoryProps"
                >
                </TreeSelect>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="课程编号" prop="code">
                <el-input
                  v-model="dataForm.code"
                  placeholder="请输入"
                  clearable
                  :style="{ width: '100%' }"
                >
                </el-input>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="课程科目" prop="subject">
                <el-select
                  v-model="dataForm.subject"
                  placeholder="请选择"
                  clearable
                  :style="{ width: '100%' }"
                >
                  <el-option
                    v-for="(item, index) in subjectOptions"
                    :key="index"
                    :label="item.fullName"
                    :value="item.enCode"
                    :disabled="item.disabled"
                  ></el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="讲师" prop="teacher">
                <custom-select
                  v-model="dataForm.teacher"
                  placeholder="请选择"
                  clearable
                >
                </custom-select>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="课时" prop="learningPeriod">
                <el-input-number
                  v-model="dataForm.learningPeriod"
                  placeholder="数字文本"
                  :step="1"
                  :precision="1"
                  controls-position="right"
                >
                </el-input-number>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="课程开发时间" prop="developTime">
                <el-date-picker
                  v-model="dataForm.developTime"
                  placeholder="请选择"
                  clearable
                  :style="{ width: '100%' }"
                  type="date"
                  format="yyyy-MM-dd"
                  value-format="timestamp"
                >
                </el-date-picker>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="适用对象" prop="applyUser">
                <el-input
                  v-model="dataForm.applyUser"
                  placeholder="请输入"
                  clearable
                  :style="{ width: '100%' }"
                >
                </el-input>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="课程类别" prop="type">
                <el-select
                  v-model="dataForm.type"
                  placeholder="请选择"
                  clearable
                  :style="{ width: '100%' }"
                >
                  <el-option
                    v-for="(item, index) in typeOptions"
                    :key="index"
                    :label="item.fullName"
                    :value="item.enCode"
                    :disabled="item.disabled"
                  ></el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="有效标志" prop="enabledMark">
                <el-select
                  v-model="dataForm.enabledMark"
                  placeholder="请选择"
                  clearable
                  :style="{ width: '100%' }"
                >
                  <el-option
                    v-for="(item, index) in enabledMarkOptions"
                    :key="index"
                    :label="item.fullName"
                    :value="item.id"
                    :disabled="item.disabled"
                  ></el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="24">
              <el-form-item label="课程介绍" prop="description">
                <el-input
                  v-model="dataForm.description"
                  placeholder="请输入"
                  :style="{ width: '100%' }"
                  true
                  type="textarea"
                  :autosize="{ minRows: 4, maxRows: 4 }"
                >
                </el-input>
              </el-form-item>
            </el-col>
          </template>
        </el-form>
      </el-row>
    </div>
  </transition>
</template>
<script>
import request from "@/utils/request";
import { getDataInterfaceRes } from "@/api/systemData/dataInterface";

export default {
  components: {},
  props: [],
  data() {
    return {
      visible: false,
      loading: false,
      isDetail: false,
      dataForm: {
        cover: [],
        name: "",
        category: "",
        code: "",
        subject: "",
        teacher: "",
        learningPeriod: 0,
        developTime: "",
        applyUser: "",
        type: "",
        enabledMark: "",
        description: "",
        creatorUserId: "",
        creatorTime: "",
        lastModifyUserId: "",
        lastModifyTime: "",
      },
      rules: {},
      categoryProps: { children: "children", label: "name", value: "id" },
      subjectOptions: [],
      typeOptions: [],
      enabledMarkOptions: [
        { fullName: "待发布", id: "0" },
        { fullName: "已发布", id: "1" },
      ],
    };
  },
  computed: {},
  watch: {},
  created() {
    this.getSubjectOptions();
    this.getTypeOptions();
  },
  mounted() {},
  methods: {
    getSubjectOptions() {
      this.$store
        .dispatch("base/getDictionaryData", {
          encode: "559344237955514629",
        })
        .then((res) => {
          this.subjectOptions = res;
        });
    },
    getTypeOptions() {
      this.$store
        .dispatch("base/getDictionaryData", {
          encode: "638986873649260037",
        })
        .then((res) => {
          this.typeOptions = res;
        });
    },
    goBack() {
      this.$emit("refresh");
    },
    clearData(data) {
      for (let key in data) {
        if (data[key] instanceof Array) {
          data[key] = [];
        } else if (data[key] instanceof Object) {
          this.clearData(data[key]);
        } else {
          data[key] = "";
        }
      }
    },
    init(id, isDetail) {
      this.dataForm.id = id || 0;
      this.visible = true;
      this.isDetail = isDetail || false;
      this.$nextTick(() => {
        this.$refs["elForm"].resetFields();
        if (this.dataForm.id) {
          this.loading = true;
          request({
            url: "/api/course/TemsCourseFace/" + this.dataForm.id,
            method: "get",
          }).then((res) => {
            this.dataInfo(res.data);
            this.loading = false;
          });
        } else {
          this.clearData(this.dataForm);
        }
      });
      this.$store.commit("generator/UPDATE_RELATION_DATA", {});
    },
    // 表单提交
    dataFormSubmit() {
      this.$refs["elForm"].validate((valid) => {
        if (valid) {
          this.request();
        }
      });
    },
    request() {
      var _data = this.dataList();
      if (!this.dataForm.id) {
        request({
          url: "/api/course/TemsCourseFace",
          method: "post",
          data: _data,
        }).then((res) => {
          this.$message({
            message: res.msg,
            type: "success",
            duration: 1000,
            onClose: () => {
              this.visible = false;
              this.$emit("refresh", true);
            },
          });
        });
      } else {
        request({
          url: "/api/course/TemsCourseFace/" + this.dataForm.id,
          method: "PUT",
          data: _data,
        }).then((res) => {
          this.$message({
            message: res.msg,
            type: "success",
            duration: 1000,
            onClose: () => {
              this.visible = false;
              this.$emit("refresh", true);
            },
          });
        });
      }
    },
    dataList() {
      var _data = JSON.parse(JSON.stringify(this.dataForm));
      _data.cover = JSON.stringify(_data.cover);
      return _data;
    },
    dataInfo(dataAll) {
      let _dataAll = dataAll;
      _dataAll.cover = JSON.parse(_dataAll.cover);
      this.dataForm = _dataAll;
    },
  },
};
</script>
